﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml.Serialization;

namespace Gamev1
{
    public class Weapon : Sprite
    {
        public Character character;
        [XmlIgnore]
        public int direction;

        public override void update(GameTime gameTime)
        {
            if (character != null)
            {
                checkDirection();
                checkPosition();
            }
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            if (direction == (int)Direction.Left)
            {
                spriteBatch.Draw(Texture, Position, null, Color, 0f, Vector2.Zero, 1f, SpriteEffects.FlipHorizontally, 0f);
            }
            else if (direction == (int)Direction.Right)
            {
                spriteBatch.Draw(Texture, Position, Color);
            }
        }

        public override void collisionWith(Sprite sprite)
        {
            if (sprite is Character)
            {
                this.character = (Character)sprite;
                character.pickWeapon(this);
                this.Position = new Vector2(sprite.Position.X, sprite.Position.Y + sprite.Rectangle.Height / 2);
            }
        }

        public void checkDirection()
        {
            this.direction = character.CurrentDirection;
        }

        public void checkPosition()
        {
            float characterCenter = character.Position.Y + character.Rectangle.Height / 2;
            if (direction == (int)Direction.Left)
            {
                this.Position = new Vector2(character.Position.X - Rectangle.Width - 1, characterCenter);
            }
            else if (direction == (int)Direction.Right)
            {
                this.Position = new Vector2(character.Position.X + character.Rectangle.Width + 1, characterCenter);
            }
        }

        public Bullet Shoot(ContentManager content)
        {
            Bullet bullet = new Bullet(this);
            bullet.animationPath = new string[1];
            bullet.animationPath[0] = "Bullets/Bullet_2";
            bullet.loadContent(content);
            return bullet;
        }

    }
}
